package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiDatabaseDocConfigBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiDatabaseDocConfigDao;
import com.cw.jeeyt.dao.model.api.ApiDatabaseDocConfigDo;
import com.cw.jeeyt.service.intf.api.ApiDatabaseDocConfigService;
import com.cw.jeeyt.service.component.ApiGenerateDatabaseExcelComponent;
import com.cw.jeeyt.service.converter.api.ApiDatabaseDocConfigConverter;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.framework.utils.SpringContextUtil;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.IdGenerator;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库文档生成配置表业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiDatabaseDocConfigServiceImpl implements ApiDatabaseDocConfigService {

    private static final Logger logger = LoggerFactory.getLogger(ApiDatabaseDocConfigServiceImpl.class);
    @Autowired
    private ApiDatabaseDocConfigDao apiDatabaseDocConfigDao;//数据库文档生成配置表数据库操作接口

    /**
     * 查询所有数据库文档生成配置表
     */
    @Override
    public List<ApiDatabaseDocConfigBo> queryApiDatabaseDocConfigList() throws Exception {
        return ApiDatabaseDocConfigConverter.doList2BoList(apiDatabaseDocConfigDao.queryApiDatabaseDocConfigList());
    }

    /**
     * 分页查询数据库文档生成配置表
     *
     * @param apiDatabaseDocConfigBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiDatabaseDocConfigBo> queryByPage(ApiDatabaseDocConfigBo apiDatabaseDocConfigBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {
        PageInfo<ApiDatabaseDocConfigDo> pageInfo = apiDatabaseDocConfigDao.queryByPage(
            ApiDatabaseDocConfigConverter.bo2Do(apiDatabaseDocConfigBo), startNum, pageSize, order);
        List<ApiDatabaseDocConfigBo> apiDatabaseDocConfigBos = ApiDatabaseDocConfigConverter.doList2BoList(
            pageInfo.getList());
        if (CollectionUtil.isNotEmpty(apiDatabaseDocConfigBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("generateDocColorStyle", "generate_doc_color_style");
            dictMap.put("generateDocType", "generate_doc_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(apiDatabaseDocConfigBos)),
                dictMap);
            apiDatabaseDocConfigBos = JSON.parseArray(jsonArray.toJSONString(), ApiDatabaseDocConfigBo.class);
        }
        return new PageInfo<>(pageInfo, apiDatabaseDocConfigBos);
    }

    /**
     * 生成数据库文档
     *
     * @param databaseId
     * @throws Exception
     */
    @Override
    public void generateDoc(Long databaseId, String generateTablePrefix, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        if (databaseId == null) {
            throw new Exception("生成数据库文档信息为空，请确认");
        }
        ApiDatabaseDocConfigBo apiDatabaseDocConfigBo = this.getApiDatabaseDocConfigByDatabaseId(databaseId);
        apiDatabaseDocConfigBo.setGenerateTablePrefix(generateTablePrefix);
        ApiGenerateDatabaseExcelComponent component = SpringContextUtil.getBean(
            ApiGenerateDatabaseExcelComponent.class);
        component.generateDatabaseDoc(apiDatabaseDocConfigBo.getDatabaseId(), apiDatabaseDocConfigBo, request,
            response);
    }

    @Override
    public ApiDatabaseDocConfigBo getApiDatabaseDocConfigByDatabaseId(Long databaseId) throws Exception {
        if (null == databaseId) {
            logger.error("--数据库id为空--");
            return null;
        }
        ApiDatabaseDocConfigBo apiDatabaseDocConfigBo = ApiDatabaseDocConfigConverter.do2Bo(
            apiDatabaseDocConfigDao.getApiDatabaseDocConfigByDatabaseId(databaseId));
        return apiDatabaseDocConfigBo;
    }

    /**
     * 根据主键查询数据库文档生成配置表信息
     *
     * @return
     */
    @Override
    public ApiDatabaseDocConfigBo getApiDatabaseDocConfigById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        ApiDatabaseDocConfigBo apiDatabaseDocConfigBo = ApiDatabaseDocConfigConverter.do2Bo(
            apiDatabaseDocConfigDao.getById(id));
        return apiDatabaseDocConfigBo;
    }

    /**
     * 新增数据库文档生成配置表
     *
     * @param apiDatabaseDocConfigBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insert(ApiDatabaseDocConfigBo apiDatabaseDocConfigBo) throws Exception {

        if (apiDatabaseDocConfigBo == null) {
            logger.error("数据库文档生成配置表为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocConfigDo apiDatabaseDocConfig = ApiDatabaseDocConfigConverter.bo2Do(apiDatabaseDocConfigBo);
        apiDatabaseDocConfig.setCreateBy(userBo.getId());
        apiDatabaseDocConfig.setCreateDate(new Date());
        apiDatabaseDocConfig.setUpdateBy(userBo.getId());
        apiDatabaseDocConfig.setUpdateDate(new Date());
        apiDatabaseDocConfig.setId(IdGenerator.nextId());
        Long id = apiDatabaseDocConfigDao.insert(apiDatabaseDocConfig);
        return id;
    }

    /**
     * 新增数据库文档生成配置表不为空的字段
     *
     * @param apiDatabaseDocConfigBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiDatabaseDocConfigBo apiDatabaseDocConfigBo) throws Exception {

        if (apiDatabaseDocConfigBo == null) {
            logger.error("数据库文档生成配置表为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocConfigDo apiDatabaseDocConfig = ApiDatabaseDocConfigConverter.bo2Do(apiDatabaseDocConfigBo);
        apiDatabaseDocConfig.setCreateBy(userBo.getId());
        apiDatabaseDocConfig.setCreateDate(new Date());
        apiDatabaseDocConfig.setUpdateBy(userBo.getId());
        apiDatabaseDocConfig.setUpdateDate(new Date());
        apiDatabaseDocConfig.setId(IdGenerator.nextId());
        Integer result = apiDatabaseDocConfigDao.insertSelective(apiDatabaseDocConfig);
        return result;
    }

    /**
     * 更新数据库文档生成配置表
     *
     * @param apiDatabaseDocConfigBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiDatabaseDocConfigBo apiDatabaseDocConfigBo) throws Exception {

        if (apiDatabaseDocConfigBo == null) {
            logger.error("数据库文档生成配置表为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocConfigDo apiDatabaseDocConfig = ApiDatabaseDocConfigConverter.bo2Do(apiDatabaseDocConfigBo);
        apiDatabaseDocConfig.setUpdateBy(userBo.getId());
        apiDatabaseDocConfig.setUpdateDate(new Date());
        apiDatabaseDocConfigDao.update(apiDatabaseDocConfig);
    }

    /**
     * 更新数据库文档生成配置表不为空的字段
     *
     * @param apiDatabaseDocConfigBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiDatabaseDocConfigBo apiDatabaseDocConfigBo) throws Exception {

        if (apiDatabaseDocConfigBo == null) {
            logger.error("数据库文档生成配置表为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocConfigDo apiDatabaseDocConfig = ApiDatabaseDocConfigConverter.bo2Do(apiDatabaseDocConfigBo);
        apiDatabaseDocConfig.setUpdateBy(userBo.getId());
        apiDatabaseDocConfig.setUpdateDate(new Date());
        apiDatabaseDocConfigDao.updateSelective(apiDatabaseDocConfig);
    }

    /**
     * 删除数据库文档生成配置表
     *
     * @param apiDatabaseDocConfigBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiDatabaseDocConfigBo apiDatabaseDocConfigBo) throws Exception {

        if (apiDatabaseDocConfigBo == null) {
            logger.error("数据库文档生成配置表为空");
            return;
        }
        ApiDatabaseDocConfigDo apiDatabaseDocConfig = ApiDatabaseDocConfigConverter.bo2Do(apiDatabaseDocConfigBo);
        apiDatabaseDocConfigDao.delete(apiDatabaseDocConfig);
    }
}
